﻿using BALayer;
using DAO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyDoiTuong
{
    public partial class QLLoaiDanhSach : Form
    {
        UserDAO userLogin = new UserDAO();
        bool ktraThem = true;
        public QLLoaiDanhSach()
        {
            InitializeComponent();
        }
        public QLLoaiDanhSach(UserDAO user)
        {
            InitializeComponent();
            this.userLogin = user;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ktraThem = true;
            setEnableControl(true);
            if (LoaiDanhSachBA.maLoaiDSMax() != -1)
            {
                txtMaLoai.Text = (LoaiDanhSachBA.maLoaiDSMax() + 1).ToString();
                txtTenLoai.Text = "";
                txtTenLoai.Focus();
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            LoaiDanhSachDAO loaiDanhSach = new LoaiDanhSachDAO();
            loaiDanhSach.MaLoai = int.Parse(txtMaLoai.Text);
            loaiDanhSach.TenLoai = txtTenLoai.Text;
            if ("".Equals(txtTenLoai.Text))
            {
                MessageBox.Show("Tên loại chưa được nhập!", "Thông Báo");
                txtTenLoai.Focus();
            }
            else
            {
                if (ktraThem)
                {
                    try
                    {
                        LoaiDanhSachBA.insertLoaiDS(loaiDanhSach);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Thông Báo");
                    }
                    
                    dataGvDanhSach.DataSource = BALayer.LoaiDanhSachBA.selectLoaiDanhSachAll();
                }
                else
                {
                    try
                    {
                        LoaiDanhSachBA.updateLoaiDS(loaiDanhSach);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Thông Báo");
                    }
                 
                    dataGvDanhSach.DataSource = BALayer.LoaiDanhSachBA.searchLoaiDanhSach(txtNhapTenLoai.Text);
                }
                int r = dataGvDanhSach.CurrentCell.RowIndex;
                if (r >= 0)
                {
                    txtMaLoai.Text = dataGvDanhSach.Rows[r].Cells[0].Value.ToString();
                    txtTenLoai.Text = dataGvDanhSach.Rows[r].Cells[1].Value.ToString();
                    btnSua.Enabled = true;
                }
                setEnableControl(false);
                txtNhapTenLoai.Focus();
            }

        }

        private void Form3_Load(object sender, EventArgs e)
        {
            setEnableControl(false);
            btnSua.Enabled = false;
            try
            {
                dataGvDanhSach.DataSource = BALayer.LoaiDanhSachBA.selectLoaiDanhSachAll();
                int r = dataGvDanhSach.CurrentCell.RowIndex;
                if (r >= 0)
                {
                    txtMaLoai.Text = dataGvDanhSach.Rows[r].Cells[0].Value.ToString();
                    txtTenLoai.Text = dataGvDanhSach.Rows[r].Cells[1].Value.ToString();
                    btnSua.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo");
            }
        }

        private void dataGvDanhSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = dataGvDanhSach.CurrentCell.RowIndex;
                txtMaLoai.Text = dataGvDanhSach.Rows[r].Cells[0].Value.ToString();
                txtTenLoai.Text = dataGvDanhSach.Rows[r].Cells[1].Value.ToString();
                setEnableControl(false);
            }
            catch (Exception)
            {
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            ktraThem = false;
            setEnableControl(true);

            txtTenLoai.Focus();
            txtMaLoai.Enabled = false;
        }

        private void btnTimLoaiDanhSach_Click(object sender, EventArgs e)
        {
            String tenLoai = txtNhapTenLoai.Text;
            try
            {
                dataGvDanhSach.DataSource = BALayer.LoaiDanhSachBA.searchLoaiDanhSach(tenLoai);
                btnSua.Enabled = false;
            }
            catch (Exception)
            {
            }
            
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            setEnableControl(false);
            int r = dataGvDanhSach.CurrentCell.RowIndex;
            txtMaLoai.Text = dataGvDanhSach.Rows[r].Cells[0].Value.ToString();
            txtTenLoai.Text = dataGvDanhSach.Rows[r].Cells[1].Value.ToString();
            txtNhapTenLoai.Focus();
        }

        private void setEnableControl(Boolean enable)
        {
            //set button
            btnHuy.Enabled = enable;
            btnLuu.Enabled = enable;
            btnSua.Enabled = !enable;
            btnThem.Enabled = !enable;

            //set textbox
            txtTenLoai.Enabled = enable;
            txtMaLoai.Enabled = enable;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Form frmMenu = new FrmManHinhChinh(userLogin);
            frmMenu.Show();
            this.Close();
        }
    }
}
